#include "SDLTexte.h"

SDLTexte::SDLTexte()
	: SDLSprite()
	, canSelectTexte(false)
	, isSelected(false)
	, isVariableMessageTexte(false)
	, beginText("")
	, endText("")
{
}

SDLTexte::SDLTexte(std::string message, std::string fontFile, int x, int y, SDL_Color* color, int fontSize)
	: SDLSprite()
	, canSelectTexte(false)
	, isSelected(false)
	, isVariableTexte(false)
	, isVariableMessageTexte(false)
	, addressVariable(nullptr)
	, adressMessage(nullptr)
	, tempMessage("")
	, beginText("")
	, endText("")
{
	SetTexture(RenderText(message, fontFile, color, fontSize));
	SetPosition(x, y);
}

SDLTexte::SDLTexte(std::string* linkMessage, std::string fontFile, int x, int y, SDL_Color* color, int fontSize)
	: SDLSprite()
	, canSelectTexte(false)
	, isSelected(false)
	, fontFile(fontFile)
	, fontSize(fontSize)
	, isVariableTexte(false)
	, isVariableMessageTexte(true)
	, addressVariable(nullptr)
	, adressMessage(nullptr)
	, tempMessage("")
	, beginText("")
	, endText("")
{

	SetPosition(x, y);
	adressMessage = linkMessage;

	theColor.r = color->r;
	theColor.g = color->g;
	theColor.b = color->b;

	if( linkMessage != nullptr)
	{
		SetTexture(RenderVariableMessageText(adressMessage, fontFile, color, fontSize));
		tempMessage = *linkMessage;
	}
}

SDLTexte::SDLTexte(int* linkVariable, std::string fontFile, int x, int y, SDL_Color* color, int fontSize)
	: SDLSprite()
	, canSelectTexte(false)
	, isSelected(false)
	, fontFile(fontFile)
	, fontSize(fontSize)
	, isVariableTexte(true)
	, isVariableMessageTexte(false)
	, addressVariable(nullptr)
	, adressMessage(nullptr)
	, tempMessage("")
	, beginText("")
	, endText("")
{

	SetPosition(x, y);
	addressVariable = linkVariable;

	theColor.r = color->r;
	theColor.g = color->g;
	theColor.b = color->b;

	if( linkVariable != nullptr)
	{
		SetTexture(RenderVariableText(linkVariable, fontFile, color, fontSize));
		temp = *linkVariable;
	}
}

SDLTexte::SDLTexte(std::string beginText, int* linkVariable, std::string endText, std::string fontFile, int x, int y, SDL_Color* color, int fontSize)
	: SDLSprite()
	, canSelectTexte(false)
	, isSelected(false)
	, fontFile(fontFile)
	, fontSize(fontSize)
	, isVariableTexte(true)
	, isVariableMessageTexte(false)
	, addressVariable(nullptr)
	, adressMessage(nullptr)
	, tempMessage("")
	, beginText(beginText)
	, endText(endText)
{
	SetPosition(x, y);
	addressVariable = linkVariable;

	theColor.r = color->r;
	theColor.g = color->g;
	theColor.b = color->b;

	if( linkVariable != nullptr)
	{
		SetTexture(RenderVariableTextAndTexte(beginText, linkVariable, endText, fontFile, color, fontSize));
		temp = *linkVariable;
	}
}

void SDLTexte::LinkVariable(int* linkVariable)
{
	if(isVariableTexte == true  &&  linkVariable != nullptr)
	{
		addressVariable = linkVariable;
		SetTexture(RenderVariableText(linkVariable, fontFile, &theColor, fontSize));
		temp = *linkVariable;
	}
}

SDLTexte::~SDLTexte()
{
	// Car cette texture est pas dans les resources.
	SDL_DestroyTexture(texture);
}


SDL_Texture* SDLTexte::RenderText(std::string message, std::string fontFile, SDL_Color* color, int fontSize)
{
	//Open the font
	TTF_Font *font = nullptr;
	font = SDLResources::GetInstance()->GetFont(fontFile, fontSize);

	if (font == nullptr)
	{
		throw std::runtime_error("Failed to load font: " + fontFile + TTF_GetError());
	}
	//Render the message to an SDL_Surface, as that's what TTF_RenderText_X returns
	SDL_Surface *surf = TTF_RenderText_Blended(font, message.c_str(), *color);
	SDL_Texture *texture = SDL_CreateTextureFromSurface(SDLRenderer::GetInstance()->GetRenderer(), surf);
	//Clean up unneeded stuff
	SDL_FreeSurface(surf);
	//TTF_CloseFont(font);
 
	return texture;
}


SDL_Texture* SDLTexte::RenderVariableMessageText(std::string* linkMessage, std::string fontFile, SDL_Color* color, int fontSize)
{
	return RenderText(*linkMessage, fontFile, color, fontSize);
}


SDL_Texture* SDLTexte::RenderVariableText(int* linkVariable, std::string fontFile, SDL_Color* color, int fontSize)
{
	std::stringstream message;
	message << *linkVariable;

	return RenderText(message.str(), fontFile, color, fontSize);
}


SDL_Texture* SDLTexte::RenderVariableTextAndTexte(std::string beginText, int* linkVariable, std::string endText, std::string fontFile, SDL_Color* color, int fontSize)
{
	std::string theText;
	theText.append(beginText.c_str());

	std::stringstream message;
	message << *linkVariable;
	theText.append((message.str()).c_str());
	theText.append(endText.c_str());

	return RenderText(theText, fontFile, color, fontSize);
}


void SDLTexte::Update()
{
	if(isVariableTexte == true  &&  addressVariable != nullptr  &&  temp != *addressVariable  &&  
		(std::strcmp(beginText.c_str(), "") != 0  ||  std::strcmp(endText.c_str(), "") != 0))
	{
		SDL_DestroyTexture(texture);
		texture = nullptr;
		srcRect.w = 0;
		srcRect.h = 0;
		SetTexture(RenderVariableTextAndTexte(beginText, addressVariable, endText, fontFile, &theColor, fontSize));
		temp = *addressVariable;
	}
	else if(isVariableTexte == true  &&  addressVariable != nullptr  &&  temp != *addressVariable)
	{
		SDL_DestroyTexture(texture);
		texture = nullptr;
		srcRect.w = 0;
		srcRect.h = 0;
		SetTexture(RenderVariableText(addressVariable, fontFile, &theColor, fontSize));
		temp = *addressVariable;
	}
	else if(isVariableMessageTexte == true  &&  adressMessage != nullptr  &&  tempMessage != *adressMessage)
	{
		SDL_DestroyTexture(texture);
		texture = nullptr;
		srcRect.w = 0;
		srcRect.h = 0;
		SetTexture(RenderVariableMessageText(adressMessage, fontFile, &theColor, fontSize));
		tempMessage = *adressMessage;
	}

	
}